package cn.changeforyou.analyzer.configFileParse;

import cn.changeforyou.analyzer.configFileParse.model.MysqlConfig;
import cn.changeforyou.utils.string.StringUtils;
import com.alibaba.fastjson.JSONObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.LinkedHashMap;
import org.yaml.snakeyaml.Yaml;

/**
 * @author zhyu
 * @date 2021/2/8 12:56
 */
public class SpringBootConfigUtils {
    
    public static MysqlConfig getMysqlConfigFromSpringBootProject(String projectPath) {
        //配置文件路径
        String resourcesPath = projectPath + File.separator + "src" + File.separator + "main" + File.separator + "resources";
        boolean isYml = false;
        String applicationYmlName = resourcesPath + File.separator +  "application.yml";
        File applicationYmlFile = new File(applicationYmlName);
        if(applicationYmlFile.exists()) {
            isYml = true;
        }
        if(isYml) {
            MysqlConfig config = getMysqlConfigFromYml(applicationYmlFile);
            if(null == config)  {
                String active = getActiveProfileFromYml(applicationYmlFile);
                if(StringUtils.isNotEmpty(active)) {
                    String application_devYmlName = resourcesPath + File.separator +  "application-" + active + ".yml";
                    File application_devYmlFile = new File(application_devYmlName);
                    if(application_devYmlFile.exists()) {
                        config = getMysqlConfigFromYml(application_devYmlFile); 
                    }
                }
            }
            if(null == config) {
                
            }
            return config;
        }else {

        }
        return null;
    }

    private static String getActiveProfileFromYml(File applicationYmlFile) {
        try {
            LinkedHashMap linkedHashMap =  new Yaml().loadAs(new FileInputStream(applicationYmlFile), LinkedHashMap.class);
            JSONObject json = new JSONObject(linkedHashMap);
            if (json.containsKey("spring")) {
                JSONObject spring = json.getJSONObject("spring");
                if (spring.containsKey("profiles")) {
                    JSONObject profiles = spring.getJSONObject("profiles");
                    if (profiles.containsKey("active")) {
                       return profiles.getString("active");
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static MysqlConfig getMysqlConfigFromYml(File applicationYmlFile) {
        try {
            LinkedHashMap linkedHashMap =  new Yaml().loadAs(new FileInputStream(applicationYmlFile), LinkedHashMap.class);
            JSONObject json = new JSONObject(linkedHashMap);
            if (json.containsKey("spring")) {
                JSONObject spring = json.getJSONObject("spring");
                if (spring.containsKey("datasource")) {
                    JSONObject datasource = spring.getJSONObject("datasource");
                    String url = datasource.getString("url");
                    String username = datasource.getString("username");
                    String password = datasource.getString("password");
                    MysqlConfig config = new MysqlConfig();
                    config.setPassword(password);
                    config.setUrl(url);
                    config.setUsername(username);
                    return config;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

}
